package cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.check;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.List;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.widget.CommolySearchView;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityInHospitalCheckHandBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPatientBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.InHospitalCheckHandPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalCheckHandViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.adapter.LvSearchAdapter;

public class InHospitalCheckHandActivity extends BaseActivity<ActivityInHospitalCheckHandBinding, InHospitalCheckHandViewModel, InHospitalCheckHandPresenter> {
    public static final int UPDATE_PROGRESS = 1;
    private MDialog mDialog;
    /**
     * TAG
     */
    private static final String TAG = "MainActivity";
    /**
     * 数据显示listview
     */
    private ListView mListView;
    /**
     * 适配器
     */
    private LvSearchAdapter adapter;
    /**
     * 通用搜索框
     */
    private CommolySearchView<InHospitalPatientBeen> mSGCommolySearchView;
    /**
     * 药单数据源
     */
    private List<InHospitalPatientBeen> mSGDatas;
    /**
     * 适配器
     */
    private LvSearchAdapter sgAdapter;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_PROGRESS:
//                    binding.progressScanGet.incrementProgressBy(countAlready / TOTAL_NUM * 100);
                    if(null != mDialog){
                        CreateDialog.dismiss(InHospitalCheckHandActivity.this);
                        mDialog.cancel();
                    }
                    if(viewModel.checkOver()){
                        finish();
                    }
                    break;
            }
        }
    };
    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);


        binding.hospitalCheckTitle.titleTextTv.setText("选择审核药品");
        binding.hospitalCheckTitle.titleBackLl.setVisibility(View.VISIBLE);

        binding.hospitalCheckTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.titleBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.hospitalCheckTitle.titleBackTv.setText("");
        initViewI();
    }



    /**
     * 初始化控件
     */
    private void initViewI() {
        mSGCommolySearchView = (CommolySearchView) findViewById(R.id.csv_show);
        mListView = (ListView) findViewById(R.id.lv_show);
        sgAdapter = new LvSearchAdapter(this, viewModel.FirstTotal);
        mListView.setAdapter(sgAdapter);
        // 设置数据源
        mSGCommolySearchView.setDatas(viewModel.FirstTotal);
        // 设置适配器
        mSGCommolySearchView.setAdapter(sgAdapter);
        // 设置筛选数据
        mSGCommolySearchView.setSearchDataListener(new CommolySearchView.SearchDatas<InHospitalPatientBeen>() {
            @Override
            public List<InHospitalPatientBeen> filterDatas(List<InHospitalPatientBeen> datas, List<InHospitalPatientBeen> filterdatas, String inputstr) {
                for (int i = 0; i < datas.size(); i++) {
//                    Log.e(TAG, "getPingYin: "+datas.get(i).getPingYin() );
                    if(datas.get(i).getPingYin().toLowerCase().contains(inputstr)){
                        filterdatas.add(datas.get(i));
                    }
                }
                return filterdatas;
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                //跳转
                InHospitalPatientBeen tag = mSGCommolySearchView.getFilterDatas().get(position);
                Intent intent=new Intent(InHospitalCheckHandActivity.this,InHospitalCheckHandDetailActivity.class);
                intent.putExtra("check",tag);
                startActivityForResult(intent, Contants.REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Contants.REQUEST_CODE:
                switch (resultCode){
                    case Contants.RESULT_HAND_SUCCESS_CODE:
                        InHospitalPatientBeen check = (InHospitalPatientBeen) data.getExtras().getSerializable("remove");
//                        Log.e(TAG, "onActivityResult1: "+ check.toString());
                        if(viewModel.checkSecond(check)){
                            mDialog = CreateDialog.successDialog(InHospitalCheckHandActivity.this, 6 ,"审核成功");
//                         mSGCommolySearchView.setDatas(viewModel.mMoveBaens);
                         sgAdapter.notifyDataSetChanged();

                         mHandler.sendEmptyMessageDelayed(UPDATE_PROGRESS,800);
                         return;
                        }
                        //手动审核的药 有但是已经被核对过了
                        CreateDialog.failDialog2(InHospitalCheckHandActivity.this);
                        break;
                    case Contants.RESULT_HAND_FAIL_CODE:
//                        setResult(Contants.RESULT_HAND_FAIL_CODE);
//                        finish();
                        break;
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        setResult(Contants.RESULT_FAIL_CODE);
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CreateDialog.dismiss(InHospitalCheckHandActivity.this);
        if (null != mDialog) {
            mDialog.cancel();
        }
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_in_hospital_check_hand;
    }

    @Override
    protected Class<InHospitalCheckHandPresenter> getPresenterClass() {
        return InHospitalCheckHandPresenter.class;
    }

    @Override
    protected Class<InHospitalCheckHandViewModel> getViewModelClass() {
        return InHospitalCheckHandViewModel.class;
    }
}
